package com.shiyingzi.common;

import java.util.Date;

import javax.servlet.http.HttpSession;

import com.shiyingzi.basics.model.BasicsEntity;
import com.shiyingzi.security.model.User;

public class EntityUtils {
	
	
	public static void initEntity(BasicsEntity entity, HttpSession session){
		User user = getSessionUser(session);
		entity.setDeleteFlag(Constants.NO);
		entity.setCreateTime(new Date());
		if(user != null){
			entity.setCreateUserId(user.getId());
		}
	}
	
	public static void modifyEntity(BasicsEntity entity, HttpSession session){
		User user = getSessionUser(session);
		entity.setModifyTime(new Date());
		if(user != null){
			entity.setModifyUserId(user.getId());
		}
	}
	
	public static void deleteEntity(BasicsEntity entity, HttpSession session){
		User user = getSessionUser(session);
		entity.setDeleteFlag(Constants.YES);
		entity.setModifyTime(new Date());
		if(user != null){
			entity.setModifyUserId(user.getId());
		}
	}
	
	public static User getSessionUser(HttpSession session){
		User user = (User)session.getAttribute(Constants.SESSION_USER);
		return user;
	}

}
